#!/usr/bin/env python3
# coding:utf-8
"""
File: color_fiter_test.py

Author:     RM_CAMP
Date:	    6/1/2022
Version     0.1.0

"""
 
from __future__ import division
import cv2
import numpy as np
 
def show_color_fiter_result(text,img,icol_t):
        img_BGR = cv2.GaussianBlur(img, (7, 7), 0) #高斯滤波
        hsv = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2HSV) #BGR转为HSV
        colorLow = np.array([icol_t[0],icol_t[1],icol_t[2]]) #导入HSV设置最低值
        colorHigh = np.array([icol_t[3],icol_t[4],icol_t[5]]) #导入HSV设置最高值
        mask = cv2.inRange(hsv, colorLow, colorHigh) #以HSV值蒙板获取目标图像
        return mask

def counter_result(color_text,img):
        kernel=np.ones((3,3),np.uint8)#腐蚀
        erosion=cv2.erode(img,kernel,iterations=8) 
        kernel_2=np.ones((3,3),np.uint8) #膨胀
        dilation=cv2.dilate(img,kernel_2,iterations=13) 
        #cv2.imshow("dilation", dilation)
        kernel=np.ones((2,2),np.uint8)#腐蚀
        erosion=cv2.erode(dilation,kernel,iterations=50) 
        ret, thresh = cv2.threshold(erosion, 150, 255, cv2.THRESH_BINARY) # 阈值处理 二值法
        thresh1 = cv2.GaussianBlur(thresh,(3,3),0)# 高斯滤波
        contours,hirearchy=cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)# 找出连通域
        area=[] #建立空数组，放连通域面积
        contours1=[]   #建立空数组，放减去最小面积的数
        mulit_area =0 #多连通区域
        for i in contours: #计算连通区域
                area.append(cv2.contourArea(i))
                if cv2.contourArea(i)>15000 and cv2.contourArea(i) < 45000:  # 计算单个球面积的数量
                        contours1.append(i)
                if cv2.contourArea(i) > 50000:   #对大的连通域除以单个球的面积
                        mulit_area =  int(cv2.contourArea(i)/34000)
        print(color_text+' : '+str(len(contours1)+mulit_area)) #计算连通域个数
        print(area)
        # cv2.imshow("color_text", thresh1)

def nothing(*arg):
        pass
 
# HSV
icol_G = (36, 82, 158, 57, 255, 255)    # Green
icol_Y = (25, 44, 46, 70, 255, 255)  # Yellow
icol_B = (89, 45, 32, 161, 255, 255)  # Blue
icol_R = (115,103 , 50, 210, 255, 255)   # Red
icol_O = (6, 103, 181, 16, 255, 255)   # Orange

capture = cv2.VideoCapture(2) # 定义摄像头
 
while True:
    ret, frame = capture.read()#摄像头读取,ret为是否成功打开摄像头,true,false。 frame为视频的每一帧图像
    if ret: # 如果有画面再执行
        # frame = cv2.flip(frame,0)   #垂直镜像操作
        frame = cv2.flip(frame,1)   #水平镜像操作   
        #显示原始图像
        cv2.imshow('frame', frame)
        #处理图像
        img_green = show_color_fiter_result('Green',frame,icol_G) #HSV绿色蒙板
        counter_result('Green',img_green) #计数
        img_yellow = show_color_fiter_result('Yellow',frame,icol_Y)
        counter_result('Yellow',img_yellow)
        img_blue = show_color_fiter_result('Blue',frame,icol_B)
        counter_result('Blue',img_blue)
        img_red = show_color_fiter_result('Red',frame,icol_R)
        counter_result('Red',img_red)
        img_organge = show_color_fiter_result('Orange',frame,icol_O)
        counter_result('Orange',img_organge)
        print("-----------------------")
	
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break
    
cv2.destroyAllWindows()
